#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include <string.h>

#define MAXLINE         4096
#define SA      struct sockaddr

//Fonction du client pour un chat
int main(int argc, char** argv)
{
    int socketfd, n;
    char recvline[MAXLINE];
    struct sockaddr_in servaddr;
    char buff[MAXLINE];

    if (argc != 3)
    {
        printf("usage: ./daytimeclient <IPAddress> [nickname]\n");
        exit(1);
    }
    // Create an Internet socket descriptor for TCP connection
    if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("Socket error");
        exit(1);
    }

    // Initialize the structure containing the server to connect to
    bzero(&servaddr, sizeof (servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(1111);
    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
    {
        printf("inet_pton error for %s\n", argv[1]);
        exit(1);
    }

    // Make connection to the server
    if (connect(socketfd, (SA *) & servaddr, sizeof (servaddr)) < 0)
    {
        perror("Connect error");
        exit(1);
    }
    //send the nickname to the server
    if ((write(socketfd, argv[2], strlen(argv[2]))) < 0)
    {
        perror("write error");
        exit(1);
    }

    //routine
    for (;;)
    {
        //initialisation de select
        fd_set rset;
        FD_ZERO(&rset);
        FD_SET(socketfd, &rset);
        FD_SET(0, &rset);
        if (select(socketfd + 1, &rset, NULL, NULL, NULL) == -1) //ici on attend un event
        {
            perror("select");
            exit(EXIT_FAILURE);
        }

        //event venant du serveur
        if (FD_ISSET(socketfd, &rset))
        {
            //on ecrit l'event
            if ((n = read(socketfd, recvline, MAXLINE - 1)) > 0)
            {
                recvline[n] = 0;
                if (fputs(recvline, stdout) == EOF)
                {
                    printf("fputs error\n");
                    exit(1);
                }
            }
            fflush(stdout); //on réactive la console
            if (n < 0)
            {
                printf("read error\n");
                exit(1);
            }
            else if (n == 0)
            {
                close(socketfd);
                return 0;
            }
        }
        else if (FD_ISSET(0, &rset)) //event de l'entrée clavier du client
        {
            fgets(buff, MAXLINE, stdin);
            if (strcmp(buff, "\n") != 0) //si ce n'est pas vide
            {
                if (strcmp(buff, "/exit\n") == 0) //si ce n'est pas /exit
                {
                    close(socketfd);
                    exit(0);
                }
                //on envoi au serveur qui s'occupe d'envoyer aux clients
                if ((write(socketfd, buff, MAXLINE)) < 0) 
                {
                    perror("write error");
                    exit(1);
                }
            }
        }
    }
    return 0;
}
